
/**
 * dateFormat(new Date(),'{y}年{M}月{d}日 {h}点{m}分{s}秒')
 * @param date
 * @param fmt
 * @returns {*}
 */
function dateFormat(date,fmt,options){
    options = options ||{};
    if(date instanceof Date){
        date = {
            y:date.getFullYear(),
            M:date.getMonth()+1,
            d:date.getDay(),
            h:date.getHours(),
            m:date.getMinutes(),
            s:date.getSeconds()
        };
        if(options.align){
            Object.keys(date).forEach(function(key){
                if(date[key]<10){
                    date[key] = '0'+date[key];
                }
            });
        }
        return replace(fmt,date);
    }else {
        return '';
    }
}

/**
 * sample 1:
 * '''js
 * var template = 'Hello {name}!'
 * var obj = {name:'John'}
 * console.log(replace(template,obj)) //out: Hello John!
 *
 * '''js
 * sample 2:
 * var template = 'Hello {user.name}!'
 * var obj = {user:{name:'Christoper Smith'}};
 * console.log(replace(template,obj))//out:Hello Christoper Smith!
 * @param template
 * @param obj
 * @returns {*}
 */
function replace(template,obj){
    var pattern = /{([_\w\.]+)}/g;
    return template.replace(pattern,function(match,p1,offset,string){
        var arr = p1.split('.');
        if(arr.length > 1){
            var i = 0;
            var len = arr.length;
            var subObj = obj[arr[0]];
            while(typeof subObj === 'object' && i < len){
                i = i + 1;
                subObj = subObj[arr[i]];
            }
            if(i === len-1){
                if(subObj === undefined){
                    return "";
                }else{
                    return subObj;
                }
            }else {
                return "";
            }
        }else{
            var ret = obj[p1];
            if(ret === undefined)
            {
                return "";
            }else{
                return ret;
            }
        }

    })
}
/**
 * comments = [[comment...],[comment...],[comment...]]
 * @param comments
 */
function commentsFormat(comments){

        //<div class="comment">
        //<p>第一条评论</p>
        //<div class="last-op">
        //<a>顶</a>
        //<a>踩</a>
        //<a href="#" class="reply" data-target="5" data-article="3">回复</a>
        //<a>分享</a>
        //</div>
        //</div>
    var tpComment =
            '<div class="comment">' +
            '{body}'+
            '<div><a href="/comment/getusercomments?id={userId}">{nickname}</a>' +
            '<span>{num}</span>'+
            '</div>'+
            '<p>{content}</p>'+
            '<div class="operation">'+
            '<a>顶</a>'+
            '<a>踩</a>'+
            '<a href="#" class="reply" data-target="{_id}" data-article="{articleId}">回复</a>'+
            '<a>分享</a>'+
            '</div>'+
            '</div>';
    var tpLast =
        '<div class="comment">' +
        '<div><a href="/comment/getusercomments?id={userId}">{nickname}</a>' +
        '<span>{date}</span>'+
        '</div>'+
        '{body}'+
        '<p>{content}</p>'+
        '<div class="last-op">'+
        '<a>顶</a>'+
        '<a>踩</a>'+
        '<a href="#" class="reply" data-target="{_id}" data-article="{articleId}">回复</a>'+
        '<a>分享</a>'+
        '</div>'+
        //'</div>'+
        '</div>';
    /*var body = '';
    for(var i=0;i<comments.length;i++){
        var comment = comments[i];
        comment.body = body;
        body = replace(tpComment,comment);
    }
    return body;*/
    if(comments.length == 0){//没有评论的时候是一个空数组
        return '';
    }
    var retComments = [];
    for(var i=0;i<comments.length;i++){
        var row = comments[i];
        var body = '';
        for(var j=row.length-1;j>=0;j--){//先格式化每个评论数组的最后一条评论
            var comment = row[j];
            comment.body = body;
            comment.num = row.length-j;
            if(j>0){
                body =replace(tpComment,comment)
            }else {
                body = replace(tpLast,comment);
            }
        }
        retComments.push(body);
    }
    return retComments.join('');
}
module.exports = {
    dateFormat:dateFormat,
    commentsFormat:commentsFormat
};